<?php
require_once '../application/library/helper/amountHelper.php';
require_once '../application/library/helper/messageHelper.php';
require_once '../application/library/helper/configHelper.php';
require_once '../application/library/helper/sessionHelper.php';
require_once '../application/library/controller/gcLBasicAction.php';
require_once '../application/library/exception/gcExceptionDealer.php';
require_once '../application/modules/goods/constant/goods_constant_constant.php';
require_once '../application/modules/goods/constant/goods_constant_templatName.php';
require_once '../application/modules/common/services/common_services_categoryTagService.php';
require_once '../application/modules/goods/services/goods_services_goodsAddEditService.php';

/**
 * 商品新增编辑Controller
 * @version   1.0
 * @author    Suda
 */
class goods_goodsAddEditController extends gcLBasicAction {

    private $objGoodsAddEditService;
    private $objCategoryTagService;

    private $arrMessages = array();
    /**
    * 新规画面初期化
    * @throws gcException
    */
    public function indexAction() {
        try {
            $this->initMenu("1");
            $this->boolUserTypeCheck("1");
            $this->pvSetGoodsPageInfo();
            $this->objCategoryTagService       = new common_services_categoryTagService();
            $this->gcView->arrGoodsFirstCATG   = $this->objCategoryTagService->pbGetGoodsFirstCategory();
            if($this->getParam("returnFg") == 1) {
                $arrSession = sessionHelper::getPageData(goods_constant_constant::GOODS_EDIT);
                $arrGoodsInfo["GOODS_CODE"] = $arrSession["GoodsInfo"]["GoodsCode"];
                $arrGoodsInfo["GOODS_NAME"] = $arrSession["GoodsInfo"]["GoodsName"];
                $arrGoodsInfo["FIRST_CATG_NO"] = $arrSession["GoodsInfo"]["goodsFirstCATG"];
                $arrGoodsInfo["SECOND_CATG_NO"] = $arrSession["GoodsInfo"]["goodsSecondCATG"];
                $arrGoodsInfo["VARIATION"] = $arrSession["GoodsInfo"]["Variation"];
                $arrGoodsInfo["SALES_PRICE"] = $arrSession["GoodsInfo"]["SellPrice"];
                $arrGoodsInfo["STOCK_NUM"] = $arrSession["GoodsInfo"]["StockNum"];
                $arrGoodsInfo["GOODS_NO"] = $arrSession["GoodsInfo"]["GoodsNo"];
                $arrSupplierInfo = $arrSession["SupplierInfo"];
            }
            $this->gcView->selGoodsFirstCATG   = $arrGoodsInfo["FIRST_CATG_NO"];
            $this->gcView->selGoodsSecondCATG  = $arrGoodsInfo["SECOND_CATG_NO"];
            $this->gcView->arrGoodsInfo = $arrGoodsInfo;
            $this->gcView->SupplierInfo = $arrSupplierInfo;
            $this->gcView->addEditFg = "1";
            $this->gcView->render(goods_constant_TemplatName::GOODS_ADD_EDIT);
        } catch (Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }

    public function goodseditAction() {
        try {
            $this->pvSetGoodsPageInfo();
            $this->objGoodsAddEditService = new goods_services_goodsAddEditService();
            if($this->getParam("returnFg") == 1) {
                $arrSession = sessionHelper::getPageData(goods_constant_constant::GOODS_EDIT);
                $arrGoodsInfo["GOODS_CODE"] = $arrSession["GoodsInfo"]["GoodsCode"];
                $arrGoodsInfo["GOODS_NAME"] = $arrSession["GoodsInfo"]["GoodsName"];
                $arrGoodsInfo["FIRST_CATG_NO"] = $arrSession["GoodsInfo"]["goodsFirstCATG"];
                $arrGoodsInfo["SECOND_CATG_NO"] = $arrSession["GoodsInfo"]["goodsSecondCATG"];
                $arrGoodsInfo["VARIATION"] = $arrSession["GoodsInfo"]["Variation"];
                $arrGoodsInfo["SALES_PRICE"] = $arrSession["GoodsInfo"]["SellPrice"];
                $arrGoodsInfo["STOCK_NUM"] = $arrSession["GoodsInfo"]["StockNum"];
                $arrGoodsInfo["GOODS_NO"] = $arrSession["GoodsInfo"]["GoodsNo"];
                $arrSupplierInfo = $arrSession["SupplierInfo"];
            } else {
                $strGoodsNo = $this->getParam("goodsNo");
                $arrGoodsInfo = $this->objGoodsAddEditService->pbGetGoodsInfo($strGoodsNo);
                $arrSupplierInfo = $this->objGoodsAddEditService->pbGetSupplierInfo($strGoodsNo);
                $arrGoodsInfo["GOODS_NO"] = $strGoodsNo;
            }
            $this->objCategoryTagService       = new common_services_categoryTagService();
            $this->gcView->arrGoodsFirstCATG   = $this->objCategoryTagService->pbGetGoodsFirstCategory();
//            $arrCondition["FirstCatg"] = "1";
            if ($arrGoodsInfo["FIRST_CATG_NO"] != "" && $arrGoodsInfo["FIRST_CATG_NO"] != null) {
                $this->gcView->arrGoodsSecondCATG = $this->objCategoryTagService->pbGetGoodsSecondCategory($arrGoodsInfo["FIRST_CATG_NO"]);
            } else {}
//            var_dump($arrGoodsInfo);exit;
            $this->gcView->addEditFg = "2";
            $this->gcView->selGoodsFirstCATG   = $arrGoodsInfo["FIRST_CATG_NO"];
            $this->gcView->selGoodsSecondCATG  = $arrGoodsInfo["SECOND_CATG_NO"];
            $this->gcView->arrGoodsInfo = $arrGoodsInfo;
            $this->gcView->GoodsNo = $arrGoodsInfo["GOODS_NO"];
            $this->gcView->SupplierInfo = $arrSupplierInfo;
            $this->gcView->render(goods_constant_TemplatName::GOODS_ADD_EDIT);
        } catch (Exception $e) {
            gcExceptionDealer::setException($e);
        }
        
    }

    /**
     * 商品删除
     */
    public function deleteAction() {
        try {
            if ($this->getRequest()->isXmlHttpRequest()) {
                $strGoodsNo = $this->getParam("GoodsNo");
                $this->objGoodsAddEditService = new goods_services_goodsAddEditService();
                $intDeleteRe = $this->objGoodsAddEditService->pbGoodsDelete($strGoodsNo);

                jsonHelper::outPutJson($intDeleteRe);
            } else {
                gcExceptionDealer::throwAjaxException();
            }
        } catch (Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }

    /**
     * 保存
     */
    public function saveAction() {
        try {
            if ($this->getRequest()->isXmlHttpRequest()) {
                $arrParameter = array("GoodsCode" => $this->getParam("GoodsCode"),
                                      "GoodsName" => $this->getParam("GoodsName"),
                                      "goodsFirstCATG" => $this->getParam("goodsFirstCATG"),
                                      "goodsSecondCATG" => $this->getParam("goodsSecondCATG"),
                                      "Variation" => $this->getParam("Variation"),
                                      "SellPrice" => $this->getParam("SellPrice"),
                                      "StockNum" => $this->getParam("StockNum"));
                $this->objGoodsAddEditService = new goods_services_goodsAddEditService();
                $arrReturn = $this->objGoodsAddEditService->pbGoodsInsert($arrParameter);
                $arrSession = sessionHelper::getPageData(goods_constant_constant::GOODS_EDIT);
                $arrSupplierInfo = $arrSession["SupplierInfo"];
                if(!empty($arrSupplierInfo)) {
                    $this->objGoodsAddEditService->pbGoodsRealatedSupplierInsert($arrSupplierInfo,$arrReturn["GoodsNo"]);
                }
                jsonHelper::outPutJson($arrReturn["InsertRe"]);
            } else {
                gcExceptionDealer::throwAjaxException();
            }
        } catch (Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }

    /**
     * 编辑
     */
    public function updateAction() {
        try {
            if ($this->getRequest()->isXmlHttpRequest()) {
                $arrParameter = array("GoodsCode" => $this->getParam("GoodsCode"),
                                      "GoodsNo" => $this->getParam("GoodsNo"),
                                      "GoodsName" => $this->getParam("GoodsName"),
                                      "goodsFirstCATG" => $this->getParam("goodsFirstCATG"),
                                      "goodsSecondCATG" => $this->getParam("goodsSecondCATG"),
                                      "Variation" => $this->getParam("Variation"),
                                      "SellPrice" => $this->getParam("SellPrice"),
                                      "StockNum" => $this->getParam("StockNum"));

                $this->objGoodsAddEditService = new goods_services_goodsAddEditService();
                $intUpdateRe = $this->objGoodsAddEditService->pbGoodsUpdate($arrParameter);
                $arrSession = sessionHelper::getPageData(goods_constant_constant::GOODS_EDIT);
                $arrSupplierInfo = $arrSession["SupplierInfo"];
                if(!empty($arrSupplierInfo)) {
                    $this->objGoodsAddEditService->pbGoodsRealatedSupplierUpdate($arrSupplierInfo,$arrParameter["GoodsNo"]);
                }
                jsonHelper::outPutJson($intUpdateRe);
            } else {
                gcExceptionDealer::throwAjaxException();
            }
        } catch (Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }

    public function setsessionAction() {
        try {
            if ($this->getRequest()->isXmlHttpRequest()) {
                $arrParameter = array("GoodsCode" => $this->getParam("GoodsCode"),
                                      "GoodsNo" => $this->getParam("GoodsNo"),
                                      "GoodsName" => $this->getParam("GoodsName"),
                                      "goodsFirstCATG" => $this->getParam("goodsFirstCATG"),
                                      "goodsSecondCATG" => $this->getParam("goodsSecondCATG"),
                                      "Variation" => $this->getParam("Variation"),
                                      "SellPrice" => $this->getParam("SellPrice"),
                                      "StockNum" => $this->getParam("StockNum"));

                $this->objGoodsService = new goods_services_goodsAddEditService();
                $arrSupplierInfo = $this->objGoodsService->pbGetSupplierInfo($arrParameter["GoodsNo"]);
                $arrSessionData["GoodsInfo"] = $arrParameter;
                $arrSessionData["SupplierInfo"] = $arrSupplierInfo;
                sessionHelper::setPageData(goods_constant_constant::GOODS_EDIT, $arrSessionData);
                $output = array();
                jsonHelper::outPutJson($output);
            } else {
                gcExceptionDealer::throwAjaxException();
            }
        } catch (Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }
    public function removesessionAction() {
        try {
            $strGoodsNo = $this->getParam("strGoodsNo");
            sessionHelper::removePageData(goods_constant_constant::GOODS_EDIT);
            jsonHelper::outPutJson($strGoodsNo);
        } catch (Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }
    /**
    * 画面信息设定
    * pvSetGoodsPageInfo
    */
    private function pvSetGoodsPageInfo() {
        $this->gcView->title    = $this->getLabel("goods_EC0002_TopPageTitle");
        $this->gcView->goodsTitle = $this->getLabel("goods_EC0002_GoodsTitle");
        $this->gcView->goodsInfo1 = $this->getLabel("goods_EC0002_GoodsInfo1");
        $this->gcView->goodsTitle2 = $this->getLabel("goods_EC0002_GoodsTitle2");
        $this->gcView->goodsCodeLabel = $this->getLabel("goods_EC0002_GoodsCode");
        $this->gcView->goodsNameLabel = $this->getLabel("goods_EC0002_GoodsName");
        $this->gcView->categoryLabel = $this->getLabel("goods_EC0002_Category");
        $this->gcView->variationLabel = $this->getLabel("goods_EC0002_Variation");
        $this->gcView->sellPriceLabel = $this->getLabel("goods_EC0002_SellPrice");
        $this->gcView->basicPriceLabel = $this->getLabel("goods_EC0002_BasicPrice");
        $this->gcView->amountUnitLabel = $this->getLabel("goods_EC0002_AmountUnit");
        $this->gcView->sellPriceLabel2 = $this->getLabel("goods_EC0002_SellPrice2");
        $this->gcView->stockLabel = $this->getLabel("goods_EC0002_Stock");
        $this->gcView->goods_EC0002_SupplierLabel = $this->getLabel("goods_EC0002_SupplierLabel");
        $this->gcView->goods_EC0002_Supplier = $this->getLabel("goods_EC0002_Supplier");
        $this->gcView->goods_EC0002_Save = $this->getLabel("goods_EC0002_Save");
        $this->gcView->goods_EC0002_Edit = $this->getLabel("goods_EC0002_Edit");
        $this->gcView->goods_EC0002_Delete = $this->getLabel("goods_EC0002_Delete");
        $this->gcView->goods_EC0002_Back = $this->getLabel("goods_EC0002_Back");
    }

}